﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Data.OracleClient;
namespace GSLMS.DAL.Oracle
{
	/// <summary>
	/// 数据访问类:SYS_USERPRIV
	/// </summary>
	public partial class SYS_USERPRIV : BaseDAL<GSLMS.Entity.TB_SYS_USERPRIV>
	{
		private readonly GSLMS.Core.Loger log = new Core.Loger();
		public SYS_USERPRIV()
		{}
		#region  Method
		/// <summary>
		///  增加一条数据
		/// </summary>
		public override bool Add(GSLMS.Entity.TB_SYS_USERPRIV model)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_EMPID", model.EMPID),
								new OracleParameter("P_USERNAME", model.USERNAME),
								new OracleParameter("P_ROLEID", model.ROLEID),
								new OracleParameter("P_CREATEBY", model.CREATEBY),
								new OracleParameter("P_CREATEDATE", model.CREATEDATE)};
  
			for (int i = 0; i < 5; i++)
			{
				parms[i].Direction = ParameterDirection.Input;
			}

			try
			{
                OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_sys.proc_userpriv_add", parms);
				flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

		/// <summary>
		///  更新一条数据
		/// </summary>
		public override bool Update(GSLMS.Entity.TB_SYS_USERPRIV model)
		{
			bool flag = false;
            //不再使用
            //OracleParameter[] parms =new OracleParameter[]{
            //                    new OracleParameter("P_EMPID", model.EMPID),
            //                    new OracleParameter("P_USERNAME", model.USERNAME),
            //                    new OracleParameter("P_ROLEID", model.ROLEID),
            //                    new OracleParameter("P_CREATEBY", model.CREATEBY),
            //                    new OracleParameter("P_CREATEDATE", model.CREATEDATE)};
  
            //for (int i = 0; i < 5; i++)
            //{
            //    parms[i].Direction = ParameterDirection.Input;
            //}

            //try
            //{
            //    OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_sys_userpriv_update", parms);
            //    flag = true;
            //}
            //catch (Exception ex)
            //{
            //    log.ExceptionHand(ex);
            //}
			return flag;
		}

		/// <summary>
		///  删除一条数据
		/// </summary>
		public override bool Delete(long ID)
		{
			bool flag = false;
            OracleParameter[] parms = new OracleParameter[]{
                                new OracleParameter("P_ID", ID)};

            parms[0].Direction = ParameterDirection.Input;

            try
            {
                OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_sys_userpriv_delete", parms);
                flag = true;
            }
            catch (Exception ex)
            {
                log.ExceptionHand(ex);
            }
			return flag;
		}

        /// <summary>
        ///  删除一条数据
        /// </summary>
        public  bool DeleteByEmpID(long EmpID,string userName,long roleID)
        {
            bool flag = false;
            OracleParameter[] parms = new OracleParameter[]{
                                new OracleParameter("P_EMPID", EmpID),
                                new OracleParameter("P_USERNAME", userName),
                                new OracleParameter("P_ROLEID", roleID)
            };

            parms[0].Direction = ParameterDirection.Input;
            parms[1].Direction = ParameterDirection.Input;
            parms[2].Direction = ParameterDirection.Input;

            try
            {
                OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_sys.proc_userpriv_delete", parms);
                flag = true;
            }
            catch (Exception ex)
            {
                log.ExceptionHand(ex);
            }
            return flag;
        }

		/// <summary>
		///DataRowToModel 
		/// </summary>
		private GSLMS.Entity.TB_SYS_USERPRIV DataRowToModel(DataRow dr)
		{
			if (dr != null && dr.ItemArray.Length > 0)
			{
				GSLMS.Entity.TB_SYS_USERPRIV model = new GSLMS.Entity.TB_SYS_USERPRIV();
				if(dr["EMPID"]!=null && dr["EMPID"].ToString()!="")
				{
					model.EMPID=OracleHelper.ToLong(dr["EMPID"].ToString());
				}
				if(dr["USERNAME"]!=null && dr["USERNAME"].ToString()!="")
				{
					model.USERNAME=dr["USERNAME"].ToString();
				}
				if(dr["ROLEID"]!=null && dr["ROLEID"].ToString()!="")
				{
					model.ROLEID=OracleHelper.ToLong(dr["ROLEID"].ToString());
				}
				if(dr["CREATEBY"]!=null && dr["CREATEBY"].ToString()!="")
				{
					model.CREATEBY=dr["CREATEBY"].ToString();
				}
				if(dr["CREATEDATE"]!=null && dr["CREATEDATE"].ToString()!="")
				{
					model.CREATEDATE=DateTime.Parse(dr["CREATEDATE"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public override GSLMS.Entity.TB_SYS_USERPRIV GetByID(long ID)
		{
			GSLMS.Entity.TB_SYS_USERPRIV sModel = new GSLMS.Entity.TB_SYS_USERPRIV();
			string sql = "";
			sql = "select * from TB_SYS_USERPRIV where id=" + ID.ToString();
			DataSet ds = OracleHelper.ExecuteDataset(sql);
			if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
			{
				sModel = DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
			return null;
			}
			return sModel;
		}

		/// <summary>
		/// 分页提取数据
		/// </summary>
		public override List<GSLMS.Entity.TB_SYS_USERPRIV> GetList(GSLMS.Entity.TB_SYS_USERPRIV searchParam)
		{
			var list=new List<GSLMS.Entity.TB_SYS_USERPRIV>();
			DataTable dt = null;
			string tblName = "TB_SYS_USERPRIV";
			string sqlWhere="";
			string strSort="";
			int pageSize=20;
			int pageIndex=1;
			string strFields="*";
			int totalCount = 0;
			int countFlag = 1;
			dt = OracleHelper.GetPageData(tblName, strFields, strSort, pageSize, pageIndex,countFlag, sqlWhere, out totalCount);
			if (dt != null && dt.Rows.Count > 0)
			{
				for (int i = 0; i < dt.Rows.Count; i++)
				{
					GSLMS.Entity.TB_SYS_USERPRIV model = new GSLMS.Entity.TB_SYS_USERPRIV();
					model = DataRowToModel(dt.Rows[i]);
					list.Add(model);
				}
			}
			else
			{
				return null;
			}
			return list;
		}

		#endregion  Method
	}
}

